<?
/************code by seven****************/
require("../../common/init.php");
require("author.php");
require("upfile.php");

$gee_cate=$product_category; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象
$gee_product = $product;

$do = '';
if (isset($_GET["do"]))$do = $_GET["do"];
$id=0; if (isset($_GET["id"])) $id = $_GET["id"];
$cate_id = 0; if(isset($_GET['cate_id'])) $cate_id = $_GET['cate_id'];

switch($do){
    case 'product_add':
        add_product();
        break;
    case 'product_cancel':
        delete();
        break;
    default:
        product_list($cate_id);

}

function product_list($cate_id){
    global $gee_cate, $gee_product, $lang, $code_array, $h, $sales_promotion_list;
    $discount_type_name = $_GET['discount_type_name'];
    $discount_value = $_GET['discount_value'];
    $promotion_id = $_GET['promotion_id'];
    $list = $gee_product->get_rows(4096,'category_id', $cate_id);
    $discount_type = $_GET['discount_type'];
    //print_r($list);
    //echo $cate_id;

    $arr_cate=$code_array['discount_type'];
    $str = '<table width="100%" height="100%">';
    foreach($list as $key => $val){
        $id = $val['id'];
        if($val['discount_type'] != 0){
                $type=$arr_cate[$val['discount_type']]['name_'.$lang];
        }else{
                $type = '';
        }

        $discount_value_a = $val['discount_value'];
        $cate_promotion_id = $val['promotion_id'];
        $promotion_list = $sales_promotion_list->get_row($cate_promotion_id);
        if($promotion_id == $cate_promotion_id){
            $str.="<tr><td>".$h->hidden('id', $id)."</td><td>".decode($val['name_'.$lang])." &nbsp; </td><td>".$type."</td><td>".$discount_value_a."</td><td>".ROOT_SALES_PROMOTION_DISCOUNT_ADD1."&nbsp;|&nbsp;<a href='javascript:void(0)' onclick=\"cate_cancel(this)\">".ROOT_SALES_PROMOTION_DISCOUNT_CANCLE."</a></td></tr>";
        }elseif($promotion_list['end_time'] >= date('Y-m-d H:i:s', time())){
            $str.="<tr><td>&nbsp;</td><td>".decode($val['name_'.$lang])." &nbsp; </td><td>".$type."</td><td>".$discount_value_a."</td><td>".ROOT_SALES_PROMOTION_DISCOUNT_ADD2."</td></tr>";
        }else{
            $str.="<tr><td>".$h->hidden('id', $id)."</td><td>".decode($val['name_'.$lang])." &nbsp; </td><td>".$h->select('type', $discount_type, $arr_cate, 'id', 'name_'.$lang, 'g gs')."</td><td>".$h->text('discount_value', $discount_value, 'g gs')."</td><td><a onclick=\"cate_add(this)\" href=\"javascript:void(0)\">".ROOT_SALES_PROMOTION_DISCOUNT_ADD."</a></td></tr>";
        }
//        $str .= '<tr height="50">';
//
//        $str .= '<td>'.$val['name_'.$lang].'</td>';
//        $str .= '<td>'.$h->select('discount_type', 0, $arr_cate, 'id', 'name_'.$lang, 'g').'</td>';
//        $str .= '<td><input type="text" name="discount_value" value="'.$discount_value.'"/></td>';
//        $str .= '<td><a>'.ROOT_SALES_PROMOTION_DISCOUNT_ADD.'</a></td>';
//
//        $str .= '</tr>';
    }
    $str .= '</table>';
    echo $str;
}// sql action / sql 动作

function add_product(){
    global $gee_product;
    $id = $_POST['id'];
    $discount_type = $_POST['discount_type'];
    $discount_type_name = $_POST['discount_type_name'];
    $discount_value = $_POST['discount_value'];
    $promotion_id = $_POST['promotion_id'];
    $arr=array(
        'discount_type'=>$discount_type,
        'promotion_id' => $promotion_id,
        'discount_value' => $discount_value,
    );
    //pre($arr); exit;
    $gee_product->update($arr, 'id', $id );
    $arr['discount_value'] = sprintf("%.2f", $arr['discount_value']);
    $arr['discount_type_name'] = $discount_type_name;
    echo json_encode($arr);
    // wheel("$url.php?promotion_id=".$promotion_id);
}

function delete()
{
    global $gee_product, $id;
    $discount_type = 0;
    $discount_type_name = '';
    $discount_value = 0;
    $promotion_id = 0;
    $arr=array(
        'discount_type'=>$discount_type,
        'promotion_id' => $promotion_id,
        'discount_value' => $discount_value,
    );
    //pre($arr); exit;
    $gee_product->update($arr, 'id', $id );
    $arr['discount_value'] = sprintf("%.2f", $arr['discount_value']);
    $arr['discount_type_name'] = $discount_type_name;
    echo json_encode($arr);
//echo $id;
}

// end article.php
